package com.sh.suhe.utildemos.views.activity.cathtml

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.sh.cathtml.adapter.MainAdapter
import com.sh.cathtml.adapter.TopAdapter
import com.sh.suhe.utildemos.R
import com.sh.suhe.utildemos.model.MainModel
import com.sh.suhe.utildemos.model.MainTypeModel
import com.sh.suhe.utildemos.utils.http.NetUtil
import kotlinx.android.synthetic.main.activity_article_main.*
import org.jsoup.Jsoup

class ArticleMainActivity : AppCompatActivity() , TopAdapter.OnClick,MainAdapter.OnClick {

    override fun toArticle(item: MainModel) {
        val intent = Intent()
        intent.putExtra("url",mainUrl+item.href)
        intent.putExtra("title",item.title)
        intent.setClass(this, ArticleActivity::class.java)
        startActivity(intent)
    }

    override fun toCatalogue(item: MainModel) {
        val intent = Intent()
        intent.putExtra("url",mainUrl+item.href)
        intent.putExtra("title",item.title)
        intent.setClass(this, CatalogueActivity::class.java)
        startActivity(intent)
    }

    private  var   TAG = this.javaClass.simpleName
    lateinit var adapter: MainAdapter
    lateinit var topAdapter: TopAdapter
    var mainUrl = "https://www.sanwenji.cn/"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_article_main)
        topAdapter = TopAdapter()
        val manager = LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.HORIZONTAL
        top_list.layoutManager = manager
        topAdapter.onClick = this
        top_list.adapter =topAdapter

        main_list.layoutManager = LinearLayoutManager(this)

        adapter = MainAdapter()
        adapter.onClick = this
        main_list.adapter = adapter

        parse(mainUrl)
    }
    val   handler =     Handler {
        when(it.what){
            1 ->{
                val data  = it.obj as  List<MainModel>
                topAdapter.notifyDataSetChanged(data)
            }
            2 ->{
                val data  = it.obj as  List<MainModel>
                adapter.notifyDataSetChanged(data,"热门推荐")
            }
            3 ->{
                val data  = it.obj as  List<MainTypeModel>
                for (typeData in data){
                    adapter.notifyDataSetChanged(typeData.mainModel,typeData.type)
                }
//
            }
        }
        true
    }

    private fun parse(url:String){

        NetUtil.request(NetUtil.RequestMethod.GET, url, HashMap(), object : NetUtil.RequestCallBack {
            override fun onSuccess(statusCode: Int, json: String?) {
                val doc = Jsoup.parse(json)
                //获取所有的博客列表集合
                val blogList = doc.select("div[class=nav-box wrapper]")
                //解析作者
                for(ele in blogList){
                    val authorEle = ele.select("a")
                    val authors = ArrayList<MainModel>()

                    for (au in authorEle){
                        val name = au.text()
                        if(name == ("网站首页") || name == ("最近更新") || name == ("网站地图")){
                            continue
                        }
                        val href = au.attr("href")
                        if (!TextUtils.isEmpty(href)){


                            val auModel = MainModel(name,href,"")
                            authors.add(auModel)
                        }
                    }
                    //通知页面刷新
                    val msg = Message()
                    msg.what = 1
                    msg.obj = authors
                    handler.sendMessage(msg)
                }
                //解析推荐
                val newList = doc.select("div[class=news-bottom]")
                //解析作者
                for(ele in newList){
                    val authorEle = ele.select("a")
                    val news = ArrayList<MainModel>()

                    for (au in authorEle){
                        val name = au.text()
                        val href = au.attr("href")
                        if (!TextUtils.isEmpty(href)){
                            val auModel = MainModel(name,href,"")
                            news.add(auModel)
                        }
                    }
                    //通知页面刷新
                    val msg = Message()
                    msg.what = 2
                    msg.obj = news
                    handler.sendMessage(msg)
                }
                //解析推荐
                val otherList = doc.select("div[class=item]")
                //解析列表
                val typeList = ArrayList<MainTypeModel>()
                for(ele in otherList){
                    val typename = ele.select("div[class=title]").first().select("a").first().text()
                    val authorEle = ele.select("ul").first().select("a")
                    val news = ArrayList<MainModel>()
                    val typeModel = MainTypeModel()
                    typeModel.type = typename
                    for (au in authorEle){
                        val name = au.text()
                        val href = au.attr("href")
                        if (!TextUtils.isEmpty(href)){
                            val auModel = MainModel(name,href,"")
                            news.add(auModel)
                        }
                    }
                    typeModel.mainModel = news
                    typeList.add(typeModel)
                }
                //通知页面刷新
                val msg = Message()
                msg.what = 3
                msg.obj = typeList
                handler.sendMessage(msg)


                Log.e(TAG,json)
            }

            override fun onFailure(statusCode: Int, errorMsg: String?) {

            }

        })
    }
}
